<?php
declare (strict_types=1);

namespace app\common\model;


use think\facade\Event;
use think\response\Json;
use app\common\tool\account\Account;

class User extends Base
{

    protected $name = 'user';

    public function getActiveTimeAttr($value)
    {
        return date('Y-m-d H:i:s', intval($value));
    }

    public static function _save($param, $admin = '')
    {
        parent::startTrans();
        try {

            if (isset($param['id']) && $param['id']) {

                if(!empty($param['password'])){
                    $param['password'] = md5($param['password']);
                }else{
                    $param['password'] = parent::where(['id' => $param['id']])->value('password'); 
                }
                parent::update($param);
                $msg = '修改会员信息，ID【' . $param['id'] . '】';
            } else {
                // $count = parent::where(['code' => $param['code'], 'is_del' => 0])->count();
                // if ($count) {
                //     return ['code' => -1, 'msg' => '会员编号已被注册'];
                // }
                $parent = parent::where(['code' => $param['parent_code'], 'is_active' => 2])->find();
                if (empty($parent)) {
                    return ['code' => -1, 'msg' => '推荐人不存在或未激活'];
                }
                $param['code'] = generate(6, '', 2);
                $param['mobile'] = trim($param['mobile']);
                $param['parent_id'] = $parent->id;
                $param['parent_name'] = $parent->nickname;
                $param['password'] = md5($param['password']);
                $param['is_active'] = 2;
                $param['active_time'] = time();
               // $param['pay_password'] = md5($param['pay_password']);

                $user = parent::create($param);

                $msg = '新增会员，编号【' . $param['code'] . '】';

                $notify = [
                    'uid' => $parent->id,
                    'type' => 2,
                    'content' => '新注册会员【' . $param['code'] . '】',
                    'source' => $user->id
                ];
                Event::trigger('UserNotify', $notify);

            }
            if ($admin) {
                Logs::adds($msg, $admin['id'], $admin['admin_name']);
            }
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功', 'url' => url('login/index')->build()];

        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function _miniAppletSave($param)
    {
        try {
            $info = parent::where('openid', $param['openid'])->where('is_del', 0)->find();

            $token = md5(json_encode($param) . $param['openid'] . time());
            if ($info) {

                if ($param['nickname'] != $info->nickname) {
                    $info->nickname = $param['nickname'];
                }
                if ($param['photo'] != $info->photo) {
                    $info->photo = $param['photo'];
                }
                $info->last_time = time();
                $info->last_ip = request()->ip();
                $info->token = $token;
                $info->save();

            } else {
                $update['nickname'] = $param['nickname'];
                $update['photo'] = $param['photo'];
                $update['openid'] = $param['openid'];
                $update['last_time'] = time();
                $update['last_ip'] = request()->ip();
                $update['token'] = $token;
                $update['code'] = generate(6, '', 2);
                parent::create($update);
            }
            return ['code' => 1, 'msg' => '登录成功', 'result' => ['token' => $token]];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function _miniAppletEdit($param, $user)
    {
        parent::startTrans();
        try {

            if (isset($param['photo']) && $param['photo']) {
                $user->photo = $param['photo'];
            }
            if (isset($param['mobile']) && $param['mobile']) {
                if ($user->mobile) {
                    return ['code' => 1, 'msg' => '您已绑定过手机号了'];
                }
                $user->mobile = $param['mobile'];
            }
            if (isset($param['parent_code']) && $param['parent_code']) {
                if ($user->parent_code) {
                    return ['code' => 1, 'msg' => '您已绑定过推荐人了'];
                }
                $parent = parent::where(['code' => $param['parent_code'], 'is_active' => 2])->find();
                if (empty($parent)) {
                    return ['code' => -1, 'msg' => '推荐人不存在或未激活'];
                }
                $user->parent_id = $parent->id;
                $user->parent_name = $parent->nickname;
                $user->parent_code = $parent->code;
                $notify = [
                    'uid' => $parent->id,
                    'type' => 2,
                    'content' => '新推荐会员【' . $user->code . '】'];
                Event::trigger('UserNotify', $notify);
            }

            $user->save();
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功'];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    public static function security($param, $user, $first = false): array
    {
        try {
            $pass = $param['type'];
            $msg = '已设置';
            if (!$first) {
                if (md5($param['old_password']) != $user->$pass) {
                    return ['code' => -1, 'msg' => '旧密码错误'];
                }
                if (md5($param['old_password']) == md5($param['password'])) {
                    return ['code' => -1, 'msg' => '新旧密码相同'];
                }
                $msg = '已修改';

            }

            $user->$pass = md5($param['password']);
            if ($pass == 'password') {
                $user->token = md5(time() . json_encode($user));
                cookie('token', null);
            }
            $user->save();


            $title = $pass == 'pay_password' ? '支付密码' : '登录密码';
            $notify = [
                'uid' => $user->id,
                'type' => 10,
                'admin_part' => 2,
                'content' => $msg . $title];
            Event::trigger('UserNotify', $notify);

            return ['code' => 1, 'msg' => $msg];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
